import { Notification } from 'element-ui';
import { listCellularStoreApi, deleteCellularInfo, deleteYfStoreApi, deleteCompeteStoreApi } from './api.js';
import { TOP_CHANNELS, CELLULAR_CLASSIFY_LIST } from '@/constants';
import dayjs from 'dayjs';
import { message } from '@/utils/util.js';
import { errorHandler } from '@/utils/request/index';

export default {
  name: 'cellular-area-config-list-table',
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    total: {
      type: Number,
      default: 0,
    },
    pageNum: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    currentPage() {
      return this.pageNum;
    },
  },
  data() {
    return {
      row: null,
      cellularStoreVisible: false, //门店列表对话框
      cellularStores: [], //门店列表
      tableLoading: false,
      isDeletingCellularRow: false,
      deleting: false,
    };
  },
  methods: {
    transfromCellularClassify(str) {
      const obj = CELLULAR_CLASSIFY_LIST.reduce((prev, curr) => {
        return { ...prev, [curr.id]: curr.label };
      }, {});
      return obj[str] || str;
    },
    onPageSizeChange(pageSize) {
      this.$emit('pageSizeChange', pageSize);
    },
    onPageNumChange(pageNum) {
      this.$emit('pageNumChange', pageNum);
    },
    onOpenCellularStoreList(row) {
      this.row = row;
      this.cellularStoreVisible = true;
      const topChannelCode = TOP_CHANNELS.find(i => i.name === row.topChannelCode)?.id;
      this.listCellularStore(row.cellularCode, topChannelCode);
    },
    onCloseCellularStoreList() {
      this.row = null;
      this.cellularStores = [];
      this.cellularStoreVisible = false;
    },

    async listCellularStore(cellularCode, topChannelCode) {
      this.tableLoading = true;
      try {
        if (cellularCode && topChannelCode) {
          this.cellularStores = await listCellularStoreApi(cellularCode, topChannelCode);
        }
        this.tableLoading = false;
      } catch (e) {
        Notification({
          title: '提示',
          message: `获取蜂窝门店列表异常`,
          type: 'error',
        });
      }
    },

    formatLastModify(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
    },

    formatCalcTime(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
    },

    // 修改蜂窝信息
    onModify(row) {
      this.$emit('openModifyModal', row);
    },
    //  操作-->删除
    async onComfirmDelete(scope) {
      try {
        this.isDeletingCellularRow = true;
        const { id } = scope.row;
        let result = await deleteCellularInfo(id);
        if (!result) {
          message('提示', '删除成功', 'success');
          this.$emit('onDelete', scope.row);
          this.isDeletingCellularRow = false;
          scope._self.$refs[`popover-${scope.$index}`].doClose();
        }
      } catch (error) {
        message('错误', error.message, 'error');
        this.isDeletingCellularRow = false;
        scope._self.$refs[`popover-${scope.$index}`].doClose();
      }
      console.log(this.isPopoverVisible);
    },
    onDelete() {
      this.isPopoverVisible = true;
      console.log('isPopoverVisible', this.isPopoverVisible);
    },
    //删除门店信息
    async onDeleteStore(value) {
      const { sortStore, storeCode, storeName } = value;
      const { cellularCode } = this.row;
      console.log('this.row', this.row);
      try {
        this.deleting = true;
        if (sortStore === 1) {
          const res = await deleteYfStoreApi(storeCode);
          if (!res) {
            this.deleting = false;
            const topChannelCode = TOP_CHANNELS.find(item => item.name === this.row.topChannelCode)?.id;
            this.listCellularStore(cellularCode, topChannelCode);
            Notification({ type: 'success', title: '提示', message: '删除成功' });
          }
          return;
        }
        const res = await deleteCompeteStoreApi(storeName);
        if (!res) {
          this.deleting = false;
          const topChannelCode = TOP_CHANNELS.find(i => i.name === this.row.topChannelCode)?.id;
          this.listCellularStore(cellularCode, topChannelCode);
          Notification({ type: 'success', title: '提示', message: '删除成功' });
        }
      } catch (error) {
        errorHandler({ error });
        this.deleting = false;
      }
    },
  },
  mounted() {},
};
